<?php
final class Config
{
    private static $root = null;
    public static function setRoot($path)
    {
        ($realpath = realpath($path)) 
        and (self::$root = $realpath) 
        or ErrorManager::trigger('NO_CONFIG_PATH');
    }
    
    private static $source = array();
    public static function load($file, $group = null, $reload = false, $overwrite = false)
    {
        if ( ! isset(self::$source[$file]) || $reload)
        {
            if (is_null($group) || true === $group)
            {
                $group = $file;
            }
            $filename = self::$root . '/' . $file . '.php';
            if (is_readable($filename))
            {
                $data = include $filename;
                ( ! isset(self::$source[$group]) or $reload) 
                and (self::$source[$group] = array());
                self::$source[$group] = $overwrite? array_merge(self::$source[$group], $data):self::$source[$group]+$data;
            }//var_dump(self::$source);//測試
            return @self::$source[$group];
        }
        return false;
    }
    public static function get($item)
    {
        $args = preg_split('/\./', $item);
        $source = self::$source;
        $retval = null;
        while ( ! empty($args))
        {
            $key = current(array_splice($args, 0,1));
            $source = @$source[$key];
            $retval = $source;
        }
        return $retval;
    }
}